/* $RCSFile: ProvidersManager.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ECommerce/PageModules/ECommerce/ProvidersManager.ascx.cs $
 * $log$
 * Revision 21 2010/08/09 11:08:12  christian.surieux
 *   Added Log comment to all .ascx .aspx .master files
 * Revision 8 2010/08/09 10:07:05  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.Core;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.UI.DataTypes;
using AddonNice.Helpers;

using AddonNice.CRMWrapper;

using AddonNice.Settings.ECommerce;
using AddonNice.CRMWrapper.ECommerce;
using AddonNice.ECommerce.UI.WebControls;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.ECommerce
{
    #region TraceSwitch for ProvidersManager
    public class ProvidersManagerTrace
	{
		static public ADNTraceSwitch	Sw	=	new ADNTraceSwitch("ProvidersManagerTrace","Local Trace Switch for ProvidersManager");
	}
	#endregion TraceSwitch for ProvidersManager
 
    /// <summary>
	/// This class is a module to manage all payments accounts and specified relations with the gateways: 
    /// one gateway could be used for several payments provider
	/// </summary>
	public partial class ProvidersManager : ECommerceModule
	{
		#region Global Implementation

        public override Guid GuidID 
		{
			get
			{
                return new Guid(EComStStr.ProvidersManagerGuid);
			}
		}

        public override bool DependsFromCustomCrmEntities
        {
            get{ return true;}
        }
        
        /// <summary>
        /// SortedList of entities/version from which this module depends, schema name is the key, version is a string in the form major.minor
        /// </summary>
        public override SortedList<string,string> RelatedCustomEntities
        {
            get
            {
                SortedList<string,string> Lst =   new SortedList<string,string>();
                Lst.Add(EComStStr.adngateway,"1.0");
                return Lst;
            }
        }

		#endregion Global Implementation
		
		#region Fields
		
		protected	GtwProviderBaseData _EMerchantData		=   null;
		
		#endregion

        #region Properties

        public string GtwType
		{
			get 
			{
                string strGtw           =   ComboGtwType.SelectedValue;
                if ( ProvidersManagerTrace.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager GtwType GET: {0}",strGtw),ProvidersManagerTrace.Sw.Info);
                return strGtw;
			}
		}
        
        
		string _GtwStringType                   =   string.Empty;
		public string GtwStringType
		{
			get 
			{
                if ( ProvidersManagerTrace.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager GtwStringType GET BEG _GtwStringType: {0}",_GtwStringType),ProvidersManagerTrace.Sw.Info);
                object ob                       =   ViewState["GtwStringType"];
                if ( (_GtwStringType == string.Empty) && ( ob != null ))
                    _GtwStringType              =   (string)ViewState["GtwStringType"];
                if ( _GtwStringType == string.Empty )
                {
                    _GtwStringType              =   EComStStr.GtwPayment;
                    ViewState["GtwStringType"]  =   _GtwStringType;
                }
				return _GtwStringType;
			}
			set 
			{
                if ( ProvidersManagerTrace.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager GtwStringType SET BEG value: {0}",value),ProvidersManagerTrace.Sw.Info);
                switch (value)
                {
                    case EComStStr.GtwSecurity:
			            _EMerchantData			=   new GtwSecurityProviderData();
                        LblGatewayType.Text     =   LocalizeHelper.Localize(EComStStr.ResourceFile, EComStStr.ResourceItem, "PROV_GTW_SECURITY", "Security Gateway");
				        _GtwStringType		    =   value;
                        break;
                    case EComStStr.GtwShipping:
			            _EMerchantData			=   new GtwShippingProviderData();
                        LblGatewayType.Text     =   LocalizeHelper.Localize(EComStStr.ResourceFile, EComStStr.ResourceItem, "PROV_GTW_SHIPPING", "Shipping Gateway");
				        _GtwStringType			=   value;
                        break;
                    default:
                    case EComStStr.GtwPayment:
			            _EMerchantData		    =   new GtwPaymentProviderData();
                        LblGatewayType.Text     =   LocalizeHelper.Localize(EComStStr.ResourceFile, EComStStr.ResourceItem, "PROV_GTW_PAYMENT", "Payment Gateway");
				        _GtwStringType			=   EComStStr.GtwPayment;
                        break;
                }
                ViewState["GtwStringType"]      =   _GtwStringType;
                if ( ProvidersManagerTrace.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager GtwStringType SET END _GtwStringType: {0}",_GtwStringType),ProvidersManagerTrace.Sw.Info);
			}
		}
		
	
		/// <summary>
		/// Purpose: Property to access the EMerchantData object.
		/// </summary>
		public GtwProviderBaseData EMerchantData
		{
			get 
			{
				return _EMerchantData;
			}
			set 
			{
				_EMerchantData = value;
			}
		}
		
		#endregion
        
        #region Ctor
		
		public ProvidersManager() 
		{
            // defaults to payment
			_EMerchantData				= new GtwPaymentProviderData();
		}
		
		
		#endregion Ctor
		
		#region Methods


        public override string StdDeleteMsg
        {
            get
            {
                return LocalizeHelper.Localize(EComStStr.ResourceFile, EComStStr.ResourceItem, "PROV_GTW_DELETETHIS","Delete this Provider ?");
             }
        }
        public override string StdDeleteTitle
        {
            get
            {
                return LocalizeHelper.Localize("DELETE2", "Delete");
            }
        }

        protected void Page_Load(object sender, System.EventArgs e) 
		{		
            if ( ModuleTraceSwitch.Sw.TraceVerbose || ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager Page_Load IsPostBack: {0}, IsInAsyncPostBack: {1}",IsPostBack,IsInAsyncPostBack),(ModuleTraceSwitch.Sw.TraceVerbose) ? ModuleTraceSwitch.Sw.Info:ProvidersManagerTrace.Sw.Info);
		    if (IsPostBack) 
                return;
			try 
			{
                RadComboBoxItem it  =   new RadComboBoxItem(LocalizeHelper.Localize(EComStStr.ResourceFile, EComStStr.ResourceItem, "PROV_GTW_PAYMENT", "Payment Gateway"),EComStStr.GtwPayment);
                ComboGtwType.Items.Add(it);
                it.Selected         =   true;
                it                  =   new RadComboBoxItem(LocalizeHelper.Localize(EComStStr.ResourceFile, EComStStr.ResourceItem, "PROV_GTW_SHIPPING", "Shipping Gateway"),EComStStr.GtwShipping);
                ComboGtwType.Items.Add(it);
                it                  =   new RadComboBoxItem(LocalizeHelper.Localize(EComStStr.ResourceFile, EComStStr.ResourceItem, "PROV_GTW_SECURITY", "Security Gateway"),EComStStr.GtwSecurity);
                ComboGtwType.Items.Add(it);
				if (IsAddable) 
				{
					btnAdd.Enabled      =   true;
				}
                else btnAdd.Enabled     =   false;

				if (IsEditable) 
				{
					btnUpdate.Enabled   =   true;
				    btnReset.Enabled    =   true;
				}
				else 
                {
                    btnUpdate.Enabled   =   false;
				    btnReset.Enabled    =   false;
                }
				if (IsDeletable) 
				{
					btnDelete.Enabled   =   true;
				}
				else btnDelete.Enabled  =   false;
				
				TbMerchantID.Enabled    = false;
                InitComboGtw();
                // May be some data has been installed by BaseProviderManager
				Populate();
                // populate the radCombo with existing providers
				PopulateProvidersList(Guid.Empty);
			}
			catch(Exception ex) 
			{
                if ( ModuleTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager Page_Load ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                throw;
			}
		}

        /// <summary>
        /// Extract from Crm Providers corresponding to type in ComboGtwType in a DataTable
        /// </summary>
        DataTable GatewayCRMTb()
        {
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager GatewayCRMTb BEG GtwType: {0}",GtwType),ProvidersManagerTrace.Sw.Info);
            DataTable Tb        =   null;
            switch (GtwType)
            {
                case EComStStr.GtwSecurity:
                    Tb          =   new GatewayCRM().GetAllSecurityGateways();
                    break;
                case EComStStr.GtwShipping:
                    Tb          =   new GatewayCRM().GetAllShippingGateways();
                    break;
                default:
                case EComStStr.GtwPayment:
                    Tb          =   new GatewayCRM().GetAllPaymentGateways();
                    break;
            }
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager GatewayCRMTb END Rows.Count: {0}",Tb.Rows.Count),ProvidersManagerTrace.Sw.Info);
            return Tb;
        }


        /// <summary>
		/// Populate the radCombo with existing providers
		/// </summary>
		protected void PopulateProvidersList(Guid SelectedProviderID) 
		{
            if ( ProvidersManagerTrace.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager PopulateProvidersList BEG SelectedProviderID: {0}.",SelectedProviderID),ProvidersManagerTrace.Sw.Info);
			try 
			{
				ComboProviders.DataSource		    =   GatewayCRMTb();
				ComboProviders.DataValueField	    =   EComStStr.adngatewayid;
				ComboProviders.DataTextField	    =   CrmStr.adnname;
				ComboProviders.DataBind();
                if ( SelectedProviderID != Guid.Empty )
                {
                    RadComboBoxItem it              =   ComboProviders.Items.FindItemByValue(SelectedProviderID.ToString());
                    if ( it != null )
                        it.Selected                 =   true;
                    if ( ProvidersManagerTrace.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager PopulateProvidersList ComboProviders SelectdItem found: {0}",it != null),ProvidersManagerTrace.Sw.Info);
                }
                else
                {
                    if ( ProvidersManagerTrace.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager PopulateProvidersList ComboProviders.Items.Count: {0}, ComboProviders.SelectedIndex: {1}",ComboProviders.Items.Count,ComboProviders.SelectedIndex),ProvidersManagerTrace.Sw.Info);
                    //  Initial load, we need to install selected provider datas in EcommerceMerchantsEdit.EMerchantData
                    // Or after delete, we install first available data in EcommerceMerchantsEdit.EMerchantData
                    if ( (ComboProviders.Items.Count > 0) && (ComboProviders.SelectedIndex != -1))
                    {
                        Guid SelGuid                =   new Guid(ComboProviders.SelectedValue);
                        if ( SelGuid != EMerchantData.MerchantID )
                            EMerchantData.GetProvider(SelGuid);
                    }
                }
			}
			catch(Exception ex) 
			{
                if ( ModuleTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager PopulateProvidersList ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]ProvidersManager PopulateProvidersList END.",ProvidersManagerTrace.Sw.Info);
		}
		
		/// <summary>
		/// Change provider type
		/// </summary>
        void ComboGtwType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager ComboGtwType_SelectedIndexChanged BEG e.Value: {0}",e.Value),ProvidersManagerTrace.Sw.Info);
			try 
			{
                // Keep order as this
				ChangeGtwType(e.Value);
                InitComboGtw();
                PopulateProvidersList(Guid.Empty);
			}
			catch(Exception ex) 
			{
                if ( ProvidersManagerTrace.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager ComboGtwType_SelectedIndexChanged ex: {0}",ex),ProvidersManagerTrace.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager ComboGtwType_SelectedIndexChanged END.",ProvidersManagerTrace.Sw.Info);
        }

		/// <summary>
		/// Loads datas for selected provider 
		/// </summary>
        void ComboProviders_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager ComboProviders_SelectedIndexChanged BEG e.Value: {0}",e.Value),ProvidersManagerTrace.Sw.Info);
			try 
			{
				EMerchantData.GetProvider(new Guid(e.Value));
			}
			catch(Exception ex) 
			{
                if ( ProvidersManagerTrace.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager ComboProviders_SelectedIndexChanged ex: {0}",ex),ProvidersManagerTrace.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager ComboProviders_SelectedIndexChanged END.",ProvidersManagerTrace.Sw.Info);
		}
		
		
        void InitComboGtw()
        {
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager InitComboGtw BEG _GtwStringType: {0}",_GtwStringType),ProvidersManagerTrace.Sw.Info);
			try 
			{
                switch (GtwStringType)
                {
                    case EComStStr.GtwSecurity:
                        ComboGateway.DataSource		=   new SecurityGtwManager().GetAllGateways().Values;
                        break;
                    case EComStStr.GtwShipping:
                        ComboGateway.DataSource		=   new ShippingGtwManager().GetAllGateways().Values;
                        break;
                    default:
                    case EComStStr.GtwPayment:
			            ComboGateway.DataSource		=   new PaymentGtwManager().GetAllGateways().Values;
                        break;
                }
				ComboGateway.DataValueField			=   "Name";
				ComboGateway.DataTextField			=   "Name";
                ComboGateway.DataBind();
			}
			catch(Exception ex) 
			{
		        if ( ProvidersManagerTrace.Sw.TraceError )
			        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager InitComboGtw ex: {0}", ex),ProvidersManagerTrace.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager InitComboGtw END _GtwStringType: {0}",_GtwStringType),ProvidersManagerTrace.Sw.Info);
        }

		public void ChangeGtwType(string newGtwType) 
		{
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager ChangeGtwType BEG newGtwType: {0}",newGtwType),ProvidersManagerTrace.Sw.Info);
            // remove handler from old object
            if ( EMerchantData != null )
                EMerchantData.DataAction-=  new EMerchantDataEventHandler(EMerchants2Data_Action);
            // reset the data container-> this generates a new object
            GtwStringType               =   newGtwType;
            // reinstall a handler
            EMerchantData.DataAction    +=  new EMerchantDataEventHandler(EMerchants2Data_Action);
            InitComboGtw(); // different from reset here
			Populate();
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager ChangeGtwType END.",ProvidersManagerTrace.Sw.Info);
		}

        /// <summary>
		/// Empty all fields by resetting the dataobject
		/// </summary>
		public void Reset() 
		{
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager Reset BEG GtwStringType: {0}, IsAddable: {1}",GtwStringType,IsAddable),ProvidersManagerTrace.Sw.Info);
            // remove handler from old object
            EMerchantData.DataAction    -=  new EMerchantDataEventHandler(EMerchants2Data_Action);
            // reset the data container-> this generates a new object
            GtwStringType               =   _GtwStringType;
            // reinstall a handler
            EMerchantData.DataAction    +=  new EMerchantDataEventHandler(EMerchants2Data_Action);
			Populate();
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager Reset END.",ProvidersManagerTrace.Sw.Info);
		}
		
		/// <summary>
		/// Fill RadCombobox with existing Providers for the gateway in EMerchantData object.
		/// </summary>
		public void Populate() 
		{
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager Populate BEG.",ProvidersManagerTrace.Sw.Info);
			try 
			{
                // Transfers values from provider dataobject to controls
				TbMerchantID.Text                               =   EMerchantData.MerchantID.ToString();
				TbName.Text                                     =   EMerchantData.Name;
				TbMerchantEmail.Text                            =	EMerchantData.MerchantEmail;
				TbTechnicalEmail.Text                           =	EMerchantData.TechnicalEmail;
				TbMetadataXml.Text                              =	EMerchantData.MetadataXml;
				TbMerchantReturnOkUrl.Text                      =	EMerchantData.OkUrl;
				TbMerchantReturnNotOkUrl.Text                   =	EMerchantData.NotOkUrl;
				TbMerchantPaymentNotificationUrl.Text           =	EMerchantData.PaymentNotificationUrl;
				TbMerchantImage.Text                            =	EMerchantData.Image;
				TbMerchantComment.Text                          =	EMerchantData.Comment;
              // select right Gtw
				string	gtwName					                =	EMerchantData.AssociatedGatewayName;
                if ( ProvidersManagerTrace.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager Populate gtwName: '{0}'.",gtwName),ProvidersManagerTrace.Sw.Info);
				if ( gtwName != string.Empty )
				{
					RadComboBoxItem it			                =	ComboGateway.FindItemByValue(gtwName);
                    if ( ProvidersManagerTrace.Sw.TraceVerbose )
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager Populate RadComboBoxItem found: {0}.",it != null ),ProvidersManagerTrace.Sw.Info);
					if ( it != null )
						it.Selected	                            =   true;
				}
			}
			catch(Exception ex) 
			{
		        if ( ProvidersManagerTrace.Sw.TraceError )
			        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager Populate ex: {0}", ex),ProvidersManagerTrace.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager Populate END.",ProvidersManagerTrace.Sw.Info);
		}
		
		/// <summary>
		/// After Insert or Update clicked, collects values from controls for Provider Data object
		/// </summary>
		private void SetProviderData() 
		{
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager SetProviderData BEG GtwStringType: {0}.",GtwStringType),ProvidersManagerTrace.Sw.Info);
			try 
			{
				EMerchantData.MerchantID                        =	new Guid(TbMerchantID.Text);
                if ( ProvidersManagerTrace.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager SetProviderData ComboGateway.SelectedValue: '{0}'.",ComboGateway.SelectedValue),ProvidersManagerTrace.Sw.Info);
				EMerchantData.AssociatedGatewayName				=	ComboGateway.SelectedValue;
				EMerchantData.Name								=	TbName.Text;
				EMerchantData.MerchantEmail					    =	TbMerchantEmail.Text;
				EMerchantData.TechnicalEmail					=	TbTechnicalEmail.Text;
				EMerchantData.MetadataXml						=	TbMetadataXml.Text;
				EMerchantData.OkUrl				                =	TbMerchantReturnOkUrl.Text;
				EMerchantData.NotOkUrl			                =	TbMerchantReturnNotOkUrl.Text;
				EMerchantData.PaymentNotificationUrl	        =	TbMerchantPaymentNotificationUrl.Text;
				EMerchantData.Image					            =	TbMerchantImage.Text;
				EMerchantData.Comment					        =	TbMerchantComment.Text;
                EMerchantData.stringType                        =   GtwStringType;
			}
			catch(Exception ex) 
			{
		        if ( ProvidersManagerTrace.Sw.TraceError )
			        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager SetProviderData ex: {0}", ex),ProvidersManagerTrace.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager SetProviderData END.",ProvidersManagerTrace.Sw.Info);
		}
		
		/// <summary>
		/// Called by GtwProviderData ; Sets the form state depending on the event.
		/// </summary>
		protected void EMerchants2Data_Action(GtwProviderBaseData sender,DataChangeEventArgs eventArgs) 
		{
	        if ( ProvidersManagerTrace.Sw.TraceVerbose )
		        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager EMerchants2Data_Action BEG MerchantID: {0}, eventArgs.Action: {1}", 
                    sender.MerchantID,eventArgs.Action),ProvidersManagerTrace.Sw.Info);
			try 
			{
				switch(eventArgs.Action) 
				{
					case Action.Selected :
						Populate();
                        break;
					case Action.Inserted :
					case Action.Updated :
						PopulateProvidersList(sender.MerchantID);
						Populate();
						break;
					
					case Action.Deleted : // read back tables and select first
			            ChangeGtwType(GtwType);
                        InitComboGtw();
                        PopulateProvidersList(Guid.Empty);
						break;
				}
			}
			catch(Exception ex) 
			{
		        if ( ProvidersManagerTrace.Sw.TraceError )
			        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager EMerchants2Data_Action ex: {0}", ex),ProvidersManagerTrace.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager EMerchants2Data_Action END.",ProvidersManagerTrace.Sw.Info);
		}
		
		
		/// <summary>
		/// Purpose: Method to Handle the Button btnAdd.Click event.
		/// </summary>
        void ProvidersManager_ADNAddInModule(object sender,ModuleEventArgs e)
        {
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager btnAdd_Click BEG.",ProvidersManagerTrace.Sw.Info);
			try 
			{
				SetProviderData();
				EMerchantData.Insert();
                e.Redirect          =   false;
			}
            catch(XmlException xex)
            {
                if ( ProvidersManagerTrace.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager btnAdd_Click invalid xml ex: {0}", xex),ProvidersManagerTrace.Sw.Info);
                lblError.Text =   string.Format("Invalid Xml: {0}",xex.Message);        
            }
			catch(Exception ex) 
			{
		        if ( ProvidersManagerTrace.Sw.TraceError )
			        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager btnAdd_Click ex: {0}", ex),ProvidersManagerTrace.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager btnAdd_Click END.",ProvidersManagerTrace.Sw.Info);
		}
		
		
		/// <summary>
		/// Purpose: Method to Handle the Button btnUpdate.Click event.
		/// </summary>
        void ProvidersManager_ADNUpdateModule(object sender,ModuleEventArgs e)
        {
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager btnUpdate_Click BEG.",ProvidersManagerTrace.Sw.Info);
			try 
			{
                if ( new Guid(TbMerchantID.Text) == Guid.Empty )
                    return;
				SetProviderData();
				EMerchantData.Update();
			}
			catch(Exception ex) 
			{
		        if ( ProvidersManagerTrace.Sw.TraceError )
			        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager btnUpdate_Click ex: {0}", ex),ProvidersManagerTrace.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager btnUpdate_Click END.",ProvidersManagerTrace.Sw.Info);
		}
		
		
		/// <summary>
		/// Purpose: Method to Handle the Button btnDelete.Click event.
		/// </summary>
        void ProvidersManager_ADNDeleteInModule(object sender,ModuleEventArgs e)
        {
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ProvidersManager btnDelete_Click BEG TbMerchantID.Text: {0}.",TbMerchantID.Text),ProvidersManagerTrace.Sw.Info);
			try 
			{
                if ( new Guid(TbMerchantID.Text) == Guid.Empty )
                    return;
				SetProviderData();
				EMerchantData.Delete();
			}
			catch(Exception ex) 
			{
		        if ( ProvidersManagerTrace.Sw.TraceError )
			        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager btnDelete_Click ex: {0}", ex),ProvidersManagerTrace.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager btnDelete_Click END.",ProvidersManagerTrace.Sw.Info);
		}
		
		
		/// <summary>
		/// Purpose: Method to Handle the Button btnReset.Click event.
		/// </summary>
		protected void btnReset_Click(object sender, EventArgs eventArgs) 
		{
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager btnReset_Click BEG.",ProvidersManagerTrace.Sw.Info);
			try 
			{
				Reset();
			}
			catch(Exception ex) 
			{
		        if ( ProvidersManagerTrace.Sw.TraceError )
			        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ProvidersManager btnReset_Click ex: {0}", ex),ProvidersManagerTrace.Sw.Info);
                throw;
			}
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager btnReset_Click END.",ProvidersManagerTrace.Sw.Info);
		}
		
        /// <summary>
        /// Inform on demo version
        /// </summary>
        void ProvidersManager_ADNSignalDemo(object sender,ModuleEventArgs e)
        {
            if ( ProvidersManagerTrace.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager ProvidersManager_ADNSignalDemo Called.",ProvidersManagerTrace.Sw.Info);
            lblError.Text           =   LocalizeHelper.Localize("DEMO_VERSION","Demo Version");
        }


        #endregion


 
        #region Std Overrides

        public override void Page_PreInit(object sender,EventArgs e)
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager Page_PreInit BEG.",ModuleTraceSwitch.Sw.Info);
            StdAddButton                                                =   btnAdd;
            StdUpdateButton                                             =   btnUpdate;
            StdDeleteButton                                             =   btnDelete;
            Settings[StStr.ModuleSettingsCRMEntityNameSettings].Value   =   EComStStr.adngateway;
            base.Page_PreInit(sender,e);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager Page_PreInit END.",ModuleTraceSwitch.Sw.Info);
        }

        override protected void OnInit(EventArgs e)
		{
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager OnInit BEG.",ModuleTraceSwitch.Sw.Info);
			InitializeComponent();
			base.OnInit(e);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ProvidersManager OnInit END.",ModuleTraceSwitch.Sw.Info);
		}

        private void InitializeComponent() 
		{
			Load                                    +=  new EventHandler(Page_Load);
            ComboGtwType.SelectedIndexChanged       +=  new RadComboBoxSelectedIndexChangedEventHandler(ComboGtwType_SelectedIndexChanged);
            ComboProviders.SelectedIndexChanged     +=  new RadComboBoxSelectedIndexChangedEventHandler(ComboProviders_SelectedIndexChanged);
			
            EMerchantData.DataAction                +=  new EMerchantDataEventHandler(EMerchants2Data_Action);
            ADNAddInModule                          +=  new PortalModuleEventHandler(ProvidersManager_ADNAddInModule);
            ADNUpdateModule                         +=  new PortalModuleEventHandler(ProvidersManager_ADNUpdateModule);
            ADNDeleteInModule                       +=  new PortalModuleEventHandler(ProvidersManager_ADNDeleteInModule);
            ADNSignalDemo                           +=  new PortalModuleEventHandler(ProvidersManager_ADNSignalDemo);
			btnReset.Click                          +=  new EventHandler(btnReset_Click);
			btnReset.CausesValidation               =   false;
		}

         #endregion Std Overrides

    }
}